package com.wangshuqi;

import com.sun.mail.imap.IMAPStore;

import javax.mail.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 邮箱工具
 *
 * @author wangshuqi
 * @date 2024/6/21 16:59
 */
public class EmailUtil {

    /**
     * 获取imap协议的邮箱会话信息
     *
     * @author wangshuqi
     * @date 2024/6/21 17:42
     */
    public static Store getPopStore(String emailAddress, String password) {
        String port = "995";   // 端口号 995:ssl  110
        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "pop3");       // 使用pop3协议
        props.setProperty("mail.pop3.host", "pop.163.com");       // pop3服务器*/
        props.setProperty("mail.pop3.port", port);           // 端口 995:ssl  110
        // SSL安全连接参数
        props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.pop3.socketFactory.fallback", "false");
        props.setProperty("mail.pop3.socketFactory.port", port);
        // 解决DecodingException: BASE64Decoder: but only got 0 before padding character (=)
        props.setProperty("mail.mime.base64.ignoreerrors", "true");

        Session session = Session.getInstance(props);
        Store store;
        try {
            store = session.getStore("pop3");
            store.connect(emailAddress, password); //163邮箱程序登录属于第三方登录所以这里的密码是163给的授权密码而并非普通的登录密码
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

        return store;
    }

    /**
     * 获取imap协议的邮箱会话信息
     *
     * @author wangshuqi
     * @date 2024/6/21 17:42
     */
    public static Store getImapStore(String emailAddress, String password) {
        String port = "993";   // 端口号 993:ssl安全端口  143
        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imap");
        props.setProperty("mail.imap.host", "imap.163.com"); // 按需要更改
        props.setProperty("mail.imap.port", port);
        // SSL安全连接参数
        props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.imap.socketFactory.fallback", "false");
        props.setProperty("mail.imap.socketFactory.port", port);
        props.setProperty("mail.mime.base64.ignoreerrors", "true");
        // 创建Session实例对象
        Session session = Session.getInstance(props);

        IMAPStore store = null;
        try {
            store = (IMAPStore) session.getStore("imap");// pop3 imap
            store.connect(emailAddress, password);
            Map<String, String> iampMap = new HashMap<>();
            //带上IMAP ID信息，由key和value组成，例如name，version，vendor，support-email等。
            // 这个value的值随便写就行
            iampMap.put("name", "18335938402");
            iampMap.put("version", "1.0.0");
            iampMap.put("vendor", "myclient");
            iampMap.put("support-email", "18335938402@163.com");
            store.id(iampMap);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
        return store;
    }


    /**
     * 获取收件箱
     *
     * @param
     * @return javax.mail.Folder
     * @author wangshuqi
     * @date 2024/6/21 17:39
     */
    public static Folder getFolder(Store store) {
        // 获得收件箱
        Folder folder;
        try {
            // 方式一：
            /*Folder defaultFolder = store.getDefaultFolder();// 默认父目录
            folder = defaultFolder.getFolder("INBOX");*/
            // 方式二：
            folder = store.getFolder("INBOX");
            /* Folder.READ_ONLY：只读权限
             * Folder.READ_WRITE：可读可写（可以修改邮件的状态）
             */
            folder.open(Folder.READ_WRITE); //打开收件箱
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
        return folder;
    }
}
